<template>
  <div>
    <!-- 表单区域 -->
    <el-form label-position="right" label-width="100px">
      <!-- 标题输入框 -->
      <el-form-item label="* 标题：">
        <el-input v-model="title" placeholder="请输入标题"></el-input>
      </el-form-item>

      <!-- 内容输入框 -->
      <el-form-item label="* 内容：">
        <el-input v-model="content" type="textarea" :rows="6" placeholder="请输入内容"></el-input>
      </el-form-item>

      <!-- 接收人选择框 -->
      <el-form-item label="* 接收人：">
        <el-select v-model="selectedUserIds" multiple placeholder="请选择接收人" style="width: 100%">
          <el-option
            v-for="user in userList"
            :key="user.id"
            :label="user.username"
            :value="user.id">
          </el-option>
        </el-select>
      </el-form-item>

      <!-- 按钮区域 -->
      <el-form-item>
        <el-button type="primary" @click="send">发送</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { MessagesentResponse } from '@/api/Messagesent'

// 模拟用户数据
type User = {
  id: number
  username: string
}

const userList = ref<User[]>([
  { id: 1, username: 'admin' },
  { id: 2, username: 'student' }
])

// 表单数据
const title = ref('')
const content = ref('')
const selectedUserIds = ref<number[]>([])

// 获取用户列表（此处为模拟）
onMounted(() => {
  // fetchUserList() 可替换为真实接口
})

// 发送消息
const send = async () => {
  if (!title.value || !content.value || selectedUserIds.value.length === 0) {
    alert('请填写完整信息')
    return
  }

  const params = {
    title: title.value,
    content: content.value,
    receiveUserIds: selectedUserIds.value
  }

  const res = await MessagesentResponse(params)
  if (res.code === 1) {
    alert('消息发送成功')
    reset()
  } else {
    alert(`发送失败：${res.message}`)
  }
}

// 重置表单
const reset = () => {
  title.value = ''
  content.value = ''
  selectedUserIds.value = []
}
</script>

<style scoped>
/* 可以添加一些自定义样式 */
</style>